package com.dysf.proxy.dynamic;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Description
 * @Author dysfzl
 * @Date 2022-01-08 12:19
 * @Copyright xx科技有限公司 Copyright (c)
 * @Version 1.0
 */
public class MyProxyCGLIB implements MethodInterceptor {

    private Object target;
    public MyProxyCGLIB(Object target) {
        this.target = target;
    }

    public Object intercept(Object proxy, Method method, Object[] args,
                            MethodProxy invocation) throws Throwable {
        //1.记录日志 2.时间统计开始 3.安全检查
        System.out.println("Cjlib动态代理开始...");
        Object retVal = invocation.invoke(target, args);
        System.out.println("Cjlib动态代理结束...");
        //4.时间统计结束
        return retVal;
    }

    public Object proxy() {
        return Enhancer.create(target.getClass(), new MyProxyCGLIB(target));
    }
}
